local fengyin = fk.CreateSkill {
  name = "cf_fengyin",
}

Fk:loadTranslationTable {
  ["cf_fengyin"] = "风引",
  [":cf_fengyin"] = "准备阶段，你可以进行一次判定（若你已受伤则改为两次），若判定结果为偶数，你可以令至多两名角色各摸两张牌并回复一点体力，若判定结果不为偶数，则你可令至多两名角色各获得一个【风引】标记。拥有【风引】标记的角色成为你与其之外的角色使用牌的目标后，你可以移除其所有【风引】标记并对该牌的使用者造成等同于【风引】标记数点伤害，然后令其弃置两张牌。",
  ["@cf_fengyin"] = "风引",
  ["#cf_fengyin-o"] = "选择至多两名角色令其回复一点体力并摸两张牌",
  ["#cf_fengyin-p"] = "选择至多两名角色令其获得【风引】标记",
  ["#cf_fengyin-d"] = "移除所有【风引】引标记对该牌使用者造成等量伤害，并令其弃两张牌。",
  ["$cf_fengyin1"] = "听凭凤引。",
  ["$cf_fengyin2"] = "风之神，请指引我们。",
}

fengyin:addEffect(fk.EventPhaseStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(fengyin.name) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = fengyin.name })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local times = (player:getLostHp() > 0) and 2 or 1
    for i = 1, times do
      local judge = {
        who = player,
        reason = fengyin.name,
        pattern = "."
      }
      room:judge(judge)
      if judge.card.number % 2 == 0 then
        local targets = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 2,
          targets = room:getAlivePlayers(),
          skill_name = fengyin.name,
          prompt = "#" .. fengyin.name .. "-o",
          cancelable = true
        })
        if #targets == 0 then return end
        for _, _target in ipairs(targets) do
          _target:drawCards(2, fengyin.name)
          room:recover({
            who = _target,
            skillName = fengyin.name,
            num = 1,
          })
        end
      else
        local targets = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 2,
          targets = room:getAlivePlayers(),
          skill_name = fengyin.name,
          cancelable = true,
          prompt = "#" .. fengyin.name .. "-p",
        })
        if #targets == 0 then return end
        for _, _target in ipairs(targets) do
          room:addPlayerMark(_target, "@cf_fengyin")
        end
      end
    end
  end
})

fengyin:addEffect(fk.TargetConfirmed, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return player:getMark("@cf_fengyin") ~= 0 and player == data.to and player ~= data.from
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local _players = table.filter(room.alive_players,
      function(element, index, array) return element:hasSkill(fengyin.name) end)
    if #_players == 0 then return end
    for _, _player in ipairs(_players) do
      if _player == data.from then return end
      if room:askToSkillInvoke(_player, { skill_name = self.name, prompt = "#" .. fengyin.name .. "-d" }) then
        room:addPlayerMark(_player, "fengyin_")
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local _player = table.find(room:getAlivePlayers(), function(element, index, array)
      return element:getMark("fengyin_")
    end)
    if not _player then return end
    room:damage({
      to = data.from,
      damage = player:getMark("@cf_fengyin"),
      from = _player,
    })
    room:askToDiscard(_player, {
      max_num = 2,
      min_num = 2,
      skill_name = fengyin.name,
      cancelable = false,
    })
    room:setPlayerMark(player, "@cf_fengyin", 0)
    room:setPlayerMark(_player, "fengyin_", 0)
  end
})

return fengyin
